// var checkAll = document.querySelector('.check-all');
// var checkOneList = document.querySelectorAll('.check-one')

// var addList = document.querySelectorAll('.add');//获取加号
// var reduceList = document.querySelectorAll('.reduce');//获取减号

// var deleteList = document.querySelectorAll('.delete');
// var deleteAll = document.querySelector('#deleteAll')

var checkAll = document.getElementsByClassName("check-all")[0];
var checkOneList = document.getElementsByClassName("check-one");

var addList = document.getElementsByClassName("add");
var reduceList = document.getElementsByClassName("reduce");

var deleteList = document.getElementsByClassName("delete");
var deleteAll = document.getElementById("deleteAll");

var selectedTotal = document.getElementById("selectedTotal");
var priceTotal = document.getElementById("priceTotal");


console.log(deleteAll);
//全选
checkAll.onclick = function () {
  console.log(this);
  var status = checkAll.checked;
  console.log(status);
  for (i = 0; i < checkOneList.length; i++) {
    var checkOne = checkOneList[i];
    checkOne.checked = status;
  }
  getTotal();
}
//单选
for (i = 0; i < checkOneList.length; i++) {
  var checkOne = checkOneList[i];
  checkOne.onclick = function () {
    // var checkedList = document.querySelectorAll('.check-one:checked');
    // // console.log(checkedList);
    // if (checkedList.length == checkOneList.length) {
    //   checkAll.checked = true;
    // } else {
    //   checkAll.checked = false;
    // }
    isAllChecked();
    getTotal();

  }
}

//加号
for (i = 0; i < addList.length; i++) {
  var add = addList[i];
  // console.log(add);
  add.onclick = function () {
    var countInput = this.previousElementSibling;
    console.log(countInput);
    var num = countInput.value;
    console.log(num);
    num++;
    countInput.value = num;

    var prented = this.parentNode;
    //获取单价
    var priceTd = prented.previousElementSibling;
    //获取小计
    var subtotalTd = prented.nextElementSibling;
    console.log(priceTd);
    console.log(subtotalTd);
    //计算小计
    var price = priceTd.textContent * 1;
    console.log(price);
    subtotalTd.textContent = (price * num).toFixed(2)

    //添加减号
    var reduce = countInput.previousElementSibling;
    // console.log(reduce);
    reduce.textContent = '-';
    getTotal();
  }
}

//减号
for (i = 0; i < reduceList.length; i++) {
  var reduce = reduceList[i];
  // console.log(reduce);
  reduce.onclick = function () {
    console.log(this);
    var countInput = this.nextElementSibling;
    console.log(countInput);
    var num = countInput.value;
    if (num == 1) {
      reduce.textContent = '';
      return false;
    }
    num--;
    if (num == 1) {
      this.textContent = '';
    }
    countInput.value = num;

    var prented = this.parentNode;
    //获取单价
    var priceTd = prented.previousElementSibling;
    //获取小计
    var subtotalTd = prented.nextElementSibling;
    console.log(priceTd);
    console.log(subtotalTd);
    //计算小计
    var price = priceTd.textContent * 1;
    console.log(price);
    subtotalTd.textContent = (price * num).toFixed(2)
    getTotal();
  }
}

//单删
for (i = 0; i < deleteList.length; i++) {
  var del = deleteList[i];
  // console.log(del);
  del.onclick = function () {
    if (confirm('是否删除该商品？')) {
      var prented = this.parentNode;
      // console.log(prented);
      var tr = prented.parentNode;
      // console.log(tr);
      // tr.style.display = "none"
      tr.remove();
      isAllChecked();
      getTotal();
    }
  }

}

//全删
deleteAll.onclick = function () {
  if (checkOneList.length > 0) {
    var checkedList = document.querySelectorAll(".check-one:checked");
    var len = checkedList.length;
    if (len > 0) {
      if (confirm("是否删除该商品")) {
        for (i = 0; i < checkOneList.length; i++) {
          let checkOne = checkOneList[i];
          // console.log(checkOne);
          if (checkOne.checked == true) {
            var prented = checkOne.parentNode;
            var tr = prented.parentNode;
            // console.log(tr);
            tr.remove();
            i--;
          }

        }
        isAllChecked();
        getTotal();
      }
    } else {
      alert("请至少选中一件商品!");
    }
  }

}

/* deleteAll.onclick = function () {
  if (checkOneList.length > 0) {
    var checkedList = document.querySelectorAll(".check-one.checked");
    var len = checkedList.length;
    // console.log(len);
    if (len > 0) {
      if (confirm("是否删除选中的商品")) {
        for (var i = 0; i < checkOneList.length; i++) {
          var checkOne = checkOneList[i];
          if (checkOne.checked) {
            var prented = checkOne.parentNode;
            var tr = prented.parentNode;
            console.log(tr);
            tr.remove();
            i--;
          }
        }
        isAllChecked();
        getTotal();
      }
    } else {
      alert("请至少选中一件商品!");
    }

  }
} */

//結算
function getTotal () {
  var sum = 0;
  var allPrice = 0;
  for (j = 0; j < checkOneList.length; j++) {
    var checkOne = checkOneList[j];
    // console.log(checkOne);
    if (checkOne.checked) {
      var prented = checkOne.parentNode;
      // console.log(prented);
      var tr = prented.parentNode;
      console.log(tr);
      // var tr = parent(parent(checkOne));
      // console.log(tr);
      var countInp = tr.getElementsByClassName("count-input")[0];
      var subtotalTd = tr.getElementsByClassName("subtotal")[0];
      var num = countInp.value * 1;
      var subtotal = subtotalTd.textContent * 1;
      // console.log(num,subtotal);

      sum += num;
      allPrice += subtotal;
    }
  }
  console.log(sum, allPrice);
  selectedTotal.textContent = sum;
  priceTotal.textContent = allPrice.toFixed(2);

}

function isAllChecked () {
  var flag = true; // 假设全部被选中   => 找到一个未选中的即可推翻假设
  for (var j = 0; j < checkOneList.length; j++) {
    var item = checkOneList[j];
    if (!item.checked) {  //item.checked == false
      flag = false;
      break;
    }
  }
  checkAll.checked = checkOneList.length > 0 ? flag : false;
}